/**
 * 说明：json解析
 * 创建：雨中磐石  from www.rili123.cn
 * 时间：2014-11-28
 * 邮箱：qqqq2900@126.com
 * QQ：290802026/1073744729
 * */

package com.framerock.baselib;



import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.Map;





public final class Json{



	/**
	 * Json解析，解析字符串：{"key":"value"};
	 * return
	 * */
	public static Map<String, String> getJsonObject(String str)
	{
		Map<String, String> map= Rock.getMap();
		if(!isJson(str))return map;
		try{
			JSONObject json = new JSONObject(str);
			Iterator<?> it 		= json.keys();
			String keys 		= "";
			String valus		= "";
			while(it.hasNext()){
				keys = (String) it.next().toString();
				valus= json.getString(keys);
				if(valus=="null")valus="";
				map.put(keys, valus);
			}
		} catch (JSONException e) {

		}
		return map;
	}

	/**
	 * 返回jsonstr{"k":"v"}
	 * */
	public static String getJsonString(Map<String, String> map)
	{
		return getJsonString(map, "");
	}
	public static String getJsonString(Map<String, String> map, String keyint)
	{
		String str = "",val = "",jg="",key;int oi = 0;
		Boolean bo = Rock.isEmpt(keyint);
		if(map !=null )for(Map.Entry<String,String> entry:map.entrySet()){
			if(oi>0)str+=",";
			key = entry.getKey();val = entry.getValue();jg  = "\"";
			if(val != null){
				val = replaceTs(val);
				if(!bo && !Rock.isEmpt(val) && Rock.contain(","+keyint+",", ","+key+","))jg="";
			}else{
				val = "";
			}
			str+="\""+key+"\":"+jg+val+jg+"";
			oi++;
		}
		if(!Rock.isEmpt(str))str="{"+str+"}";
		return str;
	}

	private static String chineseCharacterToUnicode(String str) {
		String result = "";
		for (int i = 0; i < str.length(); i++) {
			int chr1 = (char) str.charAt(i);
			if (chr1 >= 19968 && chr1 <= 171941) {//汉字范围 \u4e00-\u9fa5 (中文)
				result += "\\u" + Integer.toHexString(chr1);
			} else {
				result += str.charAt(i);
			}
		}
		return result;
	}

	public static String replaceTs(String val)
	{
		if(val==null)return val;
		val = val.replace("\"", "\\\"");
		val = val.replace("/", "\\/");
		val = val.replace("\n", "\\n");
		return val;
	}

	/**
	 * 获取提交的参数
	 * */
	public static String getJsonParams(Map<String, String> map)
	{
		String str = "";
		int oi = 0;
		if(map !=null )for(Map.Entry<String,String> entry:map.entrySet()){
			if(oi>0)str+="&";
			str+=""+entry.getKey()+"="+entry.getValue()+"";
			oi++;
		}
		return str;
	}

	/**
	 * list返回[{}]
	 * */
	public static String getListString(ArrayList<Map<String, String>> rows)
	{
		return getListString(rows, "");
	}
	public static String getListString(ArrayList<Map<String, String>> rows, String keyint)
	{
		String str = "";
		if(rows!=null)for (int i=0;i<rows.size();i++){
			if(i>0)str+=",";
			str+=getJsonString(rows.get(i),keyint);
		}
		return "["+str+"]";
	}

	public static Boolean isJson(String str)
	{
		Boolean isj = false;
		if(!Rock.isEmpt(str)) {
			int len = str.length();
			String fstr = str.substring(0, 1);
			String lstr = str.substring(len-1, len);
			if((fstr.equals("{") && lstr.equals("}")) || (fstr.equals("[") && lstr.equals("]"))){
				isj = true;
			}
		}
		return isj;
	}

	/**
	 * 创建一个json字符串
	 * */
	private String jsonString = "";
	public void putVal(String key, String val)
	{
		if(jsonString.length()>0)jsonString+=",";
		if(!isJson(val)){
			val = replaceTs(val);
			val = "\""+val+"\"";
		}
		jsonString+="\""+key+"\":"+val+"";
	}
	public void putVal(String key, Map<String, String> map)
	{
		putVal(key, getJsonString(map));
	}
	public void putVal(String key, ArrayList<Map<String, String>> rows)
	{
		putVal(key,getListString(rows));
	}
	public String getJson()
	{
		return "{"+jsonString+"}";
	}
	public void putInt(String key, int val)
	{
		if(jsonString.length()>0)jsonString+=",";
		jsonString+="\""+key+"\":"+val+"";
	}

}